package api.google.android;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class Connection {
	
    private HttpClient httpclient = new DefaultHttpClient();
    private HttpPost httppost = new HttpPost("http://vorgud2map.appspot.com");
    private final int TIMEOUT_IN_MILLIS = 10000;
    private List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    
    public Connection(String type){
    	addPostData("postType",type);
    }
	
	public String postData() {
	    try {
	    	HttpParams httpParams = new BasicHttpParams();
	    	HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_IN_MILLIS);
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	        HttpResponse request = httpclient.execute(httppost);
	        
	        if(request!=null){
	        	String repsonse = EntityUtils.toString(request.getEntity());
	            return repsonse;
	        }	        
	    } catch (ClientProtocolException e) {
	        // TODO Auto-generated catch block
	    } catch (IOException e) {
	        // TODO Auto-generated catch block
	    }
        return null;
	} 
	
	public void addPostData(String key, String value){
		nameValuePairs.add(new BasicNameValuePair(key, value));
	}
	
	public String getPostData(int location){
		return nameValuePairs.get(location).getValue();
	}

}
